<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://code.google.com/p/feather-cm
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class PaginateHelper extends Helper
{
	protected $limit = 1;
	protected $page  = 1;
	protected $count = 1;
	protected $base_link;
	
	protected $num_pages = 1;
	protected $prev_page = null;
	protected $next_page = null;
	
	/**
	 * @param $_paginate array
	 * @param $link      string
	 * @param $attr      array[optional]
	 */
	function set(array &$_paginate, $link, array $attr=null)
	{
		# config
		foreach($_paginate as $k => $v)
			$this->$k = $v;
		
		# no pages?
		if ($this->page == 1 and $this->count < $this->limit)
			return;
		
		# base link
		$this->base_link = $link.'?';
		if (!empty($attr))
		{
			foreach($attr as $k => $v)
				$attr[$k] = urlencode($k).'='.urlencode($v);
			$this->base_link .= implode('&amp;', $attr).'&amp;';
		}
		$this->base_link .= 'page=';
		
		# compute pages
		$this->num_pages = ceil((float)$this->count / $this->limit);
		$this->prev_page = ($this->page > 1) ? $this->page - 1 : null;
		$this->next_page = ($this->page < $this->num_pages) ? $this->page + 1 : null;
	}
	
	/**
	 * Is there more than one page?
	 */
	function hasPages()
	{
		return ($this->num_pages > 1);
	}
	
	/**
	 * Page: %d of %d.
	 */
	function pageResult($text=null)
	{
		if (!isset($text))
			$text = _('Page: %d of %d');
		return sprintf($text, $this->page, $this->num_pages);
	}
	
	/**
	 * Results: %d-%d of %d.
	 */
	function results($text=null)
	{
		# compute
		$start = ($this->page > 1) ? ($this->page - 1) * $this->limit + 1 : 1;
		$end   = ($this->count < $start + $this->limit) ? $this->count : $start + $this->limit - 1;
		
		# display
		if (!isset($text))
			$text = _('Results: %d-%d of %d');
		return sprintf($text, $start, $end, $this->count);
	}
	
	/**
	 * @todo Only display a limited set of pages around the current page (eg: ..4 5 6 *7 8 9 10..) .
	 */
	function numbers($separator=' ')
	{
		ob_start();
		for ($i = 1; $i <= $this->num_pages; $i++)
		{
			echo ($i != $this->page) ? $this->link($i, $this->_link.$i) : $i;
			echo $separator;
		}
		return ob_get_clean();
	}
	
	/**
	 * Link to previous page.
	 */
	function prev($text=null, $attr=null)
	{
		if (empty($text))
			$text = _('Previous');
		
		if (!is_null($this->prev_page))
			return $this->link($text, $this->base_link.$this->prev_page, $attr);
		return $text;
	}
	
	/**
	 * Link to next page.
	 */
	function next($text=null, $attr=null)
	{
		if (empty($text))
			$text = _('Next');
		
		if (!is_null($this->next_page))
			return $this->link($text, $this->base_link.$this->next_page, $attr);
		return $text;
	}
	
	/**
	 * Link to first page.
	 */
	function first($text=null, $attr=null)
	{
		if (empty($text))
			$text = _('First');
		
		if ($this->page > 1)
			return $this->link($text, $this->base_link.'1', $attr);
		return $text;
	}
	
	/**
	 * Link to last page.
	 */
	function last($text=null, $attr=null)
	{
		if (empty($text))
			$text = _('Last');
		
		if ($this->page * $this->limit < $this->count)
			return $this->link($text, $this->base_link.$this->num_pages, $attr);
		return $text;
	}
	
	/**
	 * 
	 */
	function link($text, $link, $attr=null)
	{
		if (is_string($attr))
			$attr = array('title' => $attr);
		return parent::link($text, $link, $attr);
	}
}
?>